<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">物料批次号</label>
        <el-input v-model="query.materialBatch" clearable placeholder="物料批次号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">物料编号</label>
        <el-input v-model="query.materialCode" clearable placeholder="物料编号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">物料描述</label>
        <el-input v-model="query.materialDescription" clearable placeholder="物料描述" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">容器ID</label>
        <el-input v-model="query.containerId" clearable placeholder="容器ID" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">容器编码</label>
        <el-input v-model="query.containerCode" clearable placeholder="容器编码" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">储位编码</label>
        <el-input v-model="query.storageCode" clearable placeholder="储位编码" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">冻结状态</label>
        <el-input v-model="query.frozenStatus" clearable placeholder="冻结状态" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">物料状态</label>
        <!-- <el-input v-model="query.status" clearable placeholder="物料状态(1-良品,2-不良,3-疑似良品)" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <el-select v-model="query.status" class="filter-item" clearable filterable placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option
            v-for="item in dict.semi_material_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <el-button slot="right" icon="el-icon-unlock" type="success" size="mini" class="filter-item">解冻物料</el-button>
        <el-button slot="right" icon="el-icon-lock" type="warning" size="mini" class="filter-item">冻结物料</el-button>
        <el-button slot="right" icon="el-icon-tickets" type="primary" size="mini" class="filter-item" :disabled="crud.selections.length === 0" @click="batchControl">批量更新物料状态</el-button>
      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item label="物料批次号">
            <el-input v-model="form.materialBatch" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="物料编号">
            <el-input v-model="form.materialCode" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="物料描述">
            <el-input v-model="form.materialDescription" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="容器ID">
            <el-input v-model="form.containerId" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="容器编码">
            <el-input v-model="form.containerCode" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="数量">
            <el-input v-model="form.qty" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="冻结状态">
            <el-select v-model="form.frozenStatus" class="filter-item" clearable filterable placeholder="请选择" style="width: 250px;">
              <el-option
                v-for="item in dict.semi_frozen"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="物料状态">
            <el-select v-model="form.inventoryStatus" class="filter-item" clearable filterable placeholder="请选择" style="width: 250px;">
              <el-option
                v-for="item in dict.semi_material_status"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="开始接料时间">
            <el-input v-model="form.startPullTime" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="结箱入库时间">
            <el-input v-model="form.boxInboundTime" style="width: 250px;" />
          </el-form-item> -->
          <el-form-item label="胶条截面">
            <el-input v-model="form.sealCross" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="刃高">
            <el-input v-model="form.bladeHeight" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="库区类型">
            <el-input v-model="form.warehouseZonesType" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="保质期状态">
            <el-input v-model="form.shelfStatus" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="重量">
            <el-input v-model="form.weight" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="生产时间">
            <el-input v-model="form.productionDate" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="入库时间">
            <el-input v-model="form.inboundTime" style="width: 250px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="materialBatch" label="物料批次号" />
        <el-table-column prop="materialCode" label="物料编号" />
        <el-table-column prop="materialDescription" label="物料描述" />
        <el-table-column prop="containerId" label="容器ID" />
        <el-table-column prop="containerCode" label="容器编码" />
        <el-table-column prop="storageCode" label="储位编码" />
        <el-table-column prop="qty" label="数量" />
        <el-table-column prop="frozenStatus" label="冻结状态">
          <template slot-scope="scope">
            {{ dict.label.semi_frozen[scope.row.frozenStatus] }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="物料状态">
          <template slot-scope="scope">
            {{ dict.label.semi_material_status[scope.row.inventoryStatus] }}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="startPullTime" label="开始接料时间" />
        <el-table-column prop="boxInboundTime" label="结箱入库时间" /> -->
        <el-table-column prop="sealCross" label="胶条截面" />
        <el-table-column prop="bladeHeight" label="刃高" />
        <el-table-column prop="warehouseZonesType" label="库区类型" />
        <el-table-column prop="shelfStatus" label="保质期状态">
          <template slot-scope="scope">
            {{ dict.label.semi_shelf_life_status[scope.row.shelfStatus] }}
          </template>
        </el-table-column>
        <el-table-column prop="weight" label="重量" />
        <el-table-column prop="productionDate" label="生产时间" />
        <el-table-column prop="inboundTime" label="入库时间" />
        <!-- <el-table-column label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column> -->
      </el-table>
      <!--分页组件-->
      <pagination />

      <!-- 批量更新物料状态弹窗 -->
      <el-dialog
        title="批量更新库存状态"
        :visible.sync="dialog"
        width="500px"
        :close-on-click-modal="false"
        :before-close="controlCancel"
      >
        <el-form :model="form" size="small" label-width="120px">
          <el-form-item label="更新状态">
            <el-select v-model="form.status" style="width: 250px;" placeholder="请选择">
              <el-option
                v-for="item in dict.semi_material_status"
                :key="item.value"
                :label="item.label"
                :value="Number(item.value)"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="controlCancel">取 消</el-button>
          <el-button type="primary" :loading="btn.update" @click="batchUpdate">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import crudSemiInventory from '@/api/jtWork/jtInventory'
import { batchUpdateAPI } from '@/api/jtWork/jtInventory'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
// import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, materialBatch: null, materialCode: null, materialDescription: null, containerId: null, containerCode: null, qty: null, frozenStatus: null, warehouseZonesType: null, inventoryStatus: null, startPullTime: null, boxInboundTime: null, sealCross: null, bladeHeight: null, shelfStatus: null, weight: null, productionDate: null, inboundTime: null, createTime: null, updateTime: null, createName: null, updateName: null, firstInboundTime: null }
export default {
  name: 'JtInventory',
  components: { pagination, crudOperation, rrOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: 'semiInventory', url: '/semiApi/semiInventory/inventoryView', idField: 'id', sort: 'id,desc', crudMethod: { ...crudSemiInventory },
      optShow: {
        add: true,
        edit: false,
        download: true,
        del: false,
        reset: true
      }
    })
  },
  dicts: ['semi_material_status', 'semi_container_status', 'semi_frozen', 'semi_shelf_life_status'],
  data() {
    return {
      permission: {
        add: ['admin', 'semiInventory:add', 'otherUser'],
        edit: ['admin', 'semiInventory:edit', 'otherUser'],
        del: ['admin', 'semiInventory:del', 'otherUser']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'materialBatch', display_name: '物料批次号' },
        { key: 'materialCode', display_name: '物料编号' },
        { key: 'materialDescription', display_name: '物料描述' },
        { key: 'containerId', display_name: '容器ID' },
        { key: 'frozenStatus', display_name: '冻结状态' },
        { key: 'inventoryStatus', display_name: '物料状态' }
      ],
      dialog: false,
      form: {
        status: ''
      },
      btn: {
        update: false
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    batchControl() {
      this.dialog = true
    },
    controlCancel() {
      this.dialog = false
      this.form.status = ''
    },
    async batchUpdate() {
      // console.log(this.form)
      try {
        this.btn.update = true
        const res = await batchUpdateAPI()
        if (res.code === 200) {
          this.dialog = false
          this.crud.toQuery()
          this.form.status = ''
          this.$message.success(res.message)
        } else {
          this.$message.warning(res.message)
        }
      } finally {
        this.btn.update = false
      }
    }
  }
}
</script>

<style scoped>

</style>
